package locks.dead.cooperating;

import annotation.GuardedBy;
import locks.entity.Image;

import java.util.HashSet;
import java.util.Set;

/**
 * 协作死锁
 *
 * 出租车车队
 */
public class Dispatcher {

    //全部车的集合
    @GuardedBy("this")
    private final Set<Taxi> taxis;

    //空闲的车队
    @GuardedBy("this")
    private final Set<Taxi> availableTaxis;

    public Dispatcher() {
        taxis = new HashSet<Taxi>();
        availableTaxis = new HashSet<Taxi>();
    }

    /**
     * 通知到达指定集合地点
     * @param taxi
     */
    public synchronized void notifyAvailable(Taxi taxi) {
        availableTaxis.add(taxi);
    }


    /**
     * 添加车队
     * @param taxi
     */
    public void addTaxis(Taxi taxi){
        taxis.add(taxi);
    }


    /**
     * 获取 车队中所有出租车位置
     * @return
     */
    public synchronized Image getImage() {
        Image image = new Image();
        for (Taxi t : taxis)
            image.drawMarker(t.getLocation());
        return image;
    }

}